﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class GalleryBLL
    {
        public List<Gallery> GetAll()
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.OrderByDescending(p => p.CreateDate).ToList();
            }
        }

        public List<Gallery> GetAll(bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p => p.IsEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
            }
        }
        public List<Gallery> GetAll(int top, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p => p.IsEnable == isEnable).Take(top).OrderByDescending(p => p.CreateDate).ToList();
            }
        }
        public List<Gallery> GetByCateGalleryId(int cateId, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p =>p.CateGalleryId == cateId && p.IsEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
            }
        }


        //Get by category

        public List<Gallery> GetByCategory(int idCategory, int top)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p => p.CateGalleryId == idCategory).OrderByDescending(p => p.CreateDate).Skip(0).Take(top).ToList();
            }
        }

        public List<Gallery> GetByCategory(int idCategory, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p => p.CateGalleryId == idCategory && p.IsEnable == isEnable).OrderByDescending(p => p.CreateDate).ToList();
            }
        }
        public List<Gallery> GetByCategory(int idCategory)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p => p.CateGalleryId == idCategory).OrderByDescending(p => p.CreateDate).ToList();
            }
        }
        public Gallery GetById(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p => p.Id == id).FirstOrDefault();
            }
        }

        public Gallery GetById(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                return data.Galleries.Where(p => p.Id == id && p.IsEnable == isEnable).FirstOrDefault();
            }
        }

        public void DeleteByCate(int idCategory)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                List<Gallery> obj = data.Galleries.Where(p => p.CateGalleryId == idCategory).ToList();
                if (obj != null)
                {
                    data.Galleries.DeleteAllOnSubmit(obj);
                    data.SubmitChanges();
                }
            }
        }

        public void Delete(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Gallery obj = data.Galleries.Where(p => p.Id == id).FirstOrDefault();
                if (obj != null)
                {
                    data.Galleries.DeleteOnSubmit(obj);
                    data.SubmitChanges();
                }
            }
        }
        public void Enable(int id)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Gallery obj = data.Galleries.Where(p => p.Id == id).FirstOrDefault();
                obj.IsEnable = !obj.IsEnable;

                data.SubmitChanges();
            }
        }

        public void Enable(int id, bool isEnable)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Gallery obj = data.Galleries.Where(p => p.Id == id).FirstOrDefault();
                obj.IsEnable = isEnable;

                data.SubmitChanges();
            }
        }

        public void Add(Gallery obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                data.Galleries.InsertOnSubmit(obj);
                data.SubmitChanges();
            }
        }

        //Update

        public void Update(Gallery obj)
        {
            using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
            {
                Gallery gallery = data.Galleries.Where(p => p.Id == obj.Id).FirstOrDefault();

                gallery.CreateDate = obj.CreateDate;
                gallery.CateGalleryId = obj.CateGalleryId;
                gallery.Image_1 = obj.Image_1;
                gallery.Image_2 = obj.Image_2;
                gallery.Image_3 = obj.Image_3;
                gallery.Image_4 = obj.Image_4;
                gallery.Image_5 = obj.Image_5;
                gallery.Image_6 = obj.Image_6;
                gallery.IsEnable = obj.IsEnable;

                data.SubmitChanges();
            }
        }

        //Delete
    }
}
